<?php

namespace common\models;

use Yii;
use common\models\User;

/**
 * This is the model class for table "fund".
 *
 * @property string $id
 * @property integer $user_id
 * @property string $relation_id1
 * @property string $relation_id2
 * @property integer $fund_type
 * @property string $deal_money
 * @property string $available_money
 * @property string $remark
 * @property string $create_time
 */
class Fund extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'fund';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'user_id', 'relation_id1', 'relation_id2', 'fund_type', 'deal_money', 'available_money', 'remark', 'create_time'], 'required'],
            [['id', 'user_id', 'relation_id1', 'relation_id2', 'fund_type'], 'integer'],
            [['deal_money', 'available_money'], 'number'],
            [['create_time'], 'safe'],
            [['remark'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_username' => '用户账号',
            'user_id' => '用户编号',
            'user_phone'=>'用户手机号',
            'relation_id1' => '关联id1',
            'relation_id2' => '关联id2',
            'fund_type' => '订单类型',//1、充值  2、提现 3、退款 11、加币 21、减币 31、支付 ',
            'deal_money' => '交易金额',
            'available_money' => '可用余额',
            'remark' => '备注',
            'create_time' => '创建时间',
        ];
    }

    public static function listuser(){
        $list = User::find()
            ->select(['id','phone'])
            ->asArray()
            ->all();
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $li[$v['id']] = $v['phone'];
            }
            //var_dump($li);exit;
            return $li;
        }
        
        return [];
    }

    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    public static function itemAlias ($type, $code = NULL)
    {
        $_items = [            
            'fund_type' => [
                //''  =>'显示状态',
                '1' => "充值",              
                '2' => "提现",
                '3' => "退款",
                '11' => "加币",
                '21' => "减币",
                '31' => "支付",

            ],
        ];

        if (isset($code))
            return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
        else
            return isset($_items[$type]) ? $_items[$type] : false;
    }
    /*
    * 保存前执行的操作
    */
    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            //是否是新添加
            if($this->isNewRecord) {
                
                $this->create_time = date("Y-m-d H:i:s", time());
            }else{
            }
            return true;
        } else {
            return false;
        }
    } 
}
